package com.rabbiter.oes.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.Student;
import com.rabbiter.oes.utils.PasswordEncryptionExample;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface StudentMapper extends BaseMapper<Student> {

    /**
     * 查询某学院的学生人数
     * @param collegeId
     * @return
     */
    default Long selectCountByCollegeId(Integer collegeId){
        return selectCount(Wrappers.lambdaQuery(Student.class).eq(Student::getCollegeId, collegeId));
    }

    /**
     * 分页查询所有学生
     *
     * @return List<Student>
     */
    IPage<Student> findPage(Page page, @Param("name") String name, @Param("grade") String grade, @Param("tel") String tel, @Param("collegeId") Integer collegeId, @Param("majorId") Integer majorId, @Param("clazz") String clazz);

    List<Student> findAll();

    default Long findCountByAcc (String acc) {
        LambdaQueryWrapper<Student> wrapper =new LambdaQueryWrapper<>();
        wrapper.apply("acc='" + acc.concat("'"));
        return selectCount(wrapper);
    }

    Student findById(Integer id);

    /**
     * 更新密码
     *
     * @param student
     * @return 受影响的记录条数
     */
    default int updatePwd(Student student) {
        student.setPwd(PasswordEncryptionExample.encrypted(student.getPwd()));
        return updateById(student);
    }

    Student selectByAccAndPwd(String acc);

    /**
     * 根据id修改密码
     * @param id
     * @param pwd
     * @return
     */
    default int updatePwdById(Integer id, String pwd) {
        Wrapper<Student> updateWrapper = Wrappers.lambdaUpdate(Student.class).eq(Student::getId, id).set(Student::getPwd, pwd);
        return update(new Student(), updateWrapper);
    }

}
